var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form
    ,$ = layui.jquery
    ,laydate = layui.laydate
    ,selectInput = null;
layui.use(['tableEdit', 'table', 'form', 'laydate',"selectInput"], function () {
    let layer = layui.layer //弹层
        ,table = layui.table //表格
        ,form = layui.form
        ,$ = layui.jquery
        ,laydate = layui.laydate
        ,selectInput = layui.selectInput;
    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });


    //初始
    layer.ready(function () {
        let attributesOfCustomersList=getdictionary(1,87);//客户属性
        for (let i in attributesOfCustomersList) {
            $('#attributesOfCustomers').append(new Option(attributesOfCustomersList[i].name, attributesOfCustomersList[i].code));
        }
        form.render('select');

        setCustomerInfo();
    });
    function setCustomerInfo() {
        //客户名称
        let ins = selectInput.render({
            elem: '#customerSelect',
            name: 'customerSelect',
            initValue: "",
            hasSelectIcon: true,
            placeholder: '请输入名称,只查询合作类型客户',
            data: [],
        })
        //监听输入
        ins.on('itemInput(customerSelect)', function (obj) {
            let tempData = [];
            $.ajax({
                url: "customer/getCustomerByParamList",
                data:{name:obj.value},
                async: false,
                success: function (res) {
                    if (res.status == 200) {
                        for (let i in res.data) {
                            res.data[i]["value"] = res.data[i].code;
                            tempData.push(res.data[i]);
                        }
                    }
                }
            })
            ins.addSelect(tempData)
        })

        // 监听select 选择事件
        ins.on('itemSelect(customerSelect)', function (obj) {
            let code = obj.data;
            let name = obj.textContent;
            $("#customerCode").val(code);
            $("#customerName").val(name);

            if (!isEmpty(code)){
                //获取客户信息
                $.ajax({
                    type : 'get',
                    data: {"code":code},
                    url:'customer/getCustomerByCode',
                    dataType:"json",
                    async:false,
                    success : function (data) {
                        if(data.status==200 ){
                            form.val("editor-form",{
                                "accountPeriod":data.data.accountPeriod?data.data.accountPeriod:0,
                                "creditLine":data.data.creditLine?data.data.creditLine:0,
                                "attributesOfCustomers":data.data.attributesOfCustomers,
                                "orderDate":data.data.orderDate?data.data.orderDate:null,
                            });
                        }
                    }
                });
            }

        });
    }

    form.on('submit(form-element)', function(data){
        let customerCode=data.field.customerCode;
        if (customerCode.length==0){
            layer.msg("客户名称不能为空!");
            return false;
        }
        let accountPeriod=data.field.accountPeriod;
        let creditLine=data.field.creditLine;
        let orderDate=data.field.orderDate;
        let attributesOfCustomers=data.field.attributesOfCustomers;
        $.ajax({
            type : 'get',
            data:{
                "customerCode":customerCode,
                "accountPeriod":accountPeriod,
                "creditLine":creditLine,
                "orderDate":orderDate,
                "attributesOfCustomers":attributesOfCustomers
            },
            url:'customer/updateCustomerInfo',
            dataType:"json",
            async:false,
            success : function (da) {
                if(da.status==200 ){
                    layer.msg("保存成功！");
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                }
            },
            error: function(data) {
                layer.msg("保存失败!");
            }
        });
    });


    $("#shutdown").on("click",function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    })

    function isEmpty(obj){
        if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
            return true;
        }else{
            return false;
        }
    }
})